
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">

    <title>留言板-学并思</title>

    <!-- Bootstrap core CSS -->
    <link href="./template/css/bootstrap.min.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="../../assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="jumbotron.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="../../assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">留言板v1.0</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <?php if(isset($_SESSION['username'])) { ?>
              <form class="navbar-form navbar-right loginform">
                <span class="btn btn-success"><?php echo $_SESSION['username'];  ?></span>
                <a class="btn btn-danger" href="./out.php">退出</a>
              </form>
          <?php }else{ ?>
          <form class="navbar-form navbar-right loginform">
            <div class="form-group">
              <input type="text" name="username" placeholder="用户名" value="xuebing" class="form-control">
            </div>
            <div class="form-group">
              <input type="password" name="password" placeholder="密码" value="123456" class="form-control">
            </div>
            <button type="button" class="login btn btn-success">登录</button>
            <a class="btn btn-success" href="./reg.php">注册</a>
          </form>
          <?php } ?>
          
        </div><!--/.navbar-collapse -->
      </div>
    </nav>

    <!-- Main jumbotron for a primary marketing message or call to action -->
    <div class="jumbotron">
    </div>

    <div class="container">
      <div class="row">
          <nav aria-label="...">
            <ul class="pagination">
                <?php if($up) { ?>
                  <li>
                      <a href="./index.php?p=<?php echo $up; ?>" aria-label="Previous">
                          <span aria-hidden="true">上一页</span></a>
                  </li>
                <?php } ?>

                <?php for ($i=$start; $i <=$end ; $i++) { ?>
                    <li <?php if($i==$p){ echo "class='active'";} ?>><a href="./index.php?p=<?php echo $i; ?>"><?php echo $i; ?></a></li>
                <?php } ?>
                
                <?php if($down) { ?>
                <li>
                    <a href="./index.php?p=<?php echo $down; ?>" aria-label="Next">
                        <span aria-hidden="true">下一页</span></a>
                </li>
                <?php } ?>

            </ul>
        </nav>
      </div>
      <!-- Example row of columns -->
      <div class="row" id="main">
      	<?php foreach ($oldData as $k => $v) { ?>
        <div class="col-md-4">
          <h2><?php echo $v['username']; ?></h2>
          <p><?php echo $v['content']; ?></p>
          <p>(<?php echo Ftime($v['ctime']); ?>)</p>
          <p>
          	<a class="btn btn-danger del" id='<?php echo $k; ?>' >删除</a>
          	<a class="btn btn-success" href="./edit.php?k=<?php echo $k; ?>">修改</a>
          </p>
        </div>
        <?php } ?>
      </div>
      <div class="row" style="text-align: center;margin-top: 30px;">
          <span id="more" class="btn btn-success">加载更多</span> 
      </div>

      <div class="row" style="margin-top: 50px;">
			<form class="form-inline msgform" action="./add.php" method="post">
			  <button type="button" class="addmsg btn btn-primary">发布</button>
			  <div class="col-md-12" style="margin-top: 20px;">
			  		<textarea name="content" class="form-control" style="width: 100%" rows="3"></textarea>
			  </div>
			  
			</form>
      </div>

      <hr>

      <footer>
        <p>&copy; 2016 Company, Inc.</p>
      </footer>
    </div> <!-- /container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>

    <script>

        $('.del').click(function(event) {
            id = $(this).attr('id');

            $.ajax({
              url: './delmsg.php',
              type: 'POST',
              dataType: 'json',
              data: {id:id},
            })
            .done(function(res) {
              if(res.error==1){
                alert(res.msg);
              }else{
                console.log(res);
                location.reload();
              }
            })
            .fail(function() {
              console.log("error");
            })
        });

        $('.addmsg').click(function(event) {
           postdata = $('.msgform').serialize();

           $.ajax({
              url: './addmsg.php',
              type: 'POST',
              dataType: 'json',
              data: postdata,
            })
            .done(function(res) {
              if(res.error==1){
                alert(res.msg);
              }else{
                console.log(res);
                location.reload();
              }
            })
            .fail(function() {
              console.log("error");
            })


        });

        $('.login').click(function(event) {
            postdata = $('.loginform').serialize();
            $.ajax({
              url: './checkLogin.php',
              type: 'POST',
              dataType: 'json',
              data: postdata,
            })
            .done(function(res) {
              if(res.error==1){
                alert(res.msg);
              }else{
                location.reload();
              }
            })
            .fail(function() {
              console.log("error");
            })
        });
      
        p = 1;
        $('#more').click(function(event) {
            p++;
            $.ajax({
              url: './getmore.php',
              type: 'POST',
              dataType: 'json',
              data: {p:p},
            })
            .done(function(res) {
              console.log(res);
              if(res.code==1){
                alert(res.msg);
              }else{
                datalist = res.data;
                for (var i in datalist) {
                    html = '<div class="col-md-4"><h2>'+datalist[i].username+'</h2><p>'+datalist[i].content+'</p><p>('+datalist[i].ctime+')</p><p><a class="btn btn-danger" href="./del.php?k='+i+'">删除</a><a class="btn btn-success" href="./edit.php?k='+i+'">修改</a></p></div>';

                    $('#main').append(html);

                }
              }
            })
            .fail(function() {
              console.log("error");
            })
            .always(function() {
              console.log("complete");
            });
        });

    </script>
  </body>
</html>
